package cn.controller;

import cn.basic.BaseController;
import cn.dao.BorrowDAO;
import cn.factory.DAOFactory;
import cn.model.TBorrow;
import cn.model.VBorrowEquipment;
import cn.model.VUserRole;
import cn.util.Expression;
import cn.util.Toolsutil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 信息数据交互
 * @author WZG
 * @version 2020-2-30
 */
@Controller
@RequestMapping(value="/borrow")
public class BorrowController  extends BaseController {

    /*
     * 实现一条信息的添加
     * @throws IOException
     */
    @RequestMapping(value="/addborrow")
    public void AddBorrow(
            Integer equipid,
            String reasons,
            HttpServletRequest request,
            HttpServletResponse response,
            Model model) throws IOException {

        VUserRole user = (VUserRole)session.getAttribute("loginuser");
        TBorrow borrow = new TBorrow();
        borrow.setEquipid(equipid);
        borrow.setBorrowTime(Toolsutil.getdatetimes());
        borrow.setReasons(reasons);
        borrow.setUserid(user.getUserid());
        borrow.setAudit(1);
        BorrowDAO borrowao = DAOFactory.getBorrowDAO();
        if (borrowao.addBorrow(borrow) > 0) {
            dt.code = dt.FLAG_SUCC;
            dt.msg = "添加成功";
        } else {
            dt.code = dt.FLAG_FAIL;
            dt.msg = "添加失败";
        }
        closeout();
    }

    /*
     * 实现一条信息的删除
     * @param borrowid 编号
     * @throws IOException
     */
    @RequestMapping(value="/deleteborrow")
    public void deleteborrow(
            String borrowid,
            HttpServletRequest request,
            HttpServletResponse response) throws IOException{

        BorrowDAO borrowao = DAOFactory.getBorrowDAO();
        if (borrowid !=null && !borrowid.equals("")){
            borrowao.deleteBorrow(Integer.parseInt(borrowid));
            dt.code = dt.FLAG_SUCC;
            dt.msg = "删除成功";
        }else {
            dt.code = dt.FLAG_FAIL;
            dt.msg = "删除失败 ";
        }
        closeout();
    }
    /*
     * 实现一条信息修改
     * @param
     * @throws IOException
     */
    @RequestMapping(value="/modifyborrow")
    public void modifyborrow(
            Integer borrowid,
            Integer equipid,
            String reasons,
            Integer audit,
            HttpServletRequest request,
            HttpServletResponse response,
            Model model) throws IOException{

        VUserRole user = (VUserRole)session.getAttribute("loginuser");
        TBorrow borrow = new TBorrow();
        borrow.setBorrowid(borrowid);
        borrow.setEquipid(equipid);
        borrow.setReasons(reasons);
        borrow.setUserid(user.getUserid());
        borrow.setAudit(audit);

        BorrowDAO borrowao = DAOFactory.getBorrowDAO();
        if (borrowao.modifyBorrow(borrow)) {
            dt.code = dt.FLAG_SUCC;
            dt.msg = "修改成功";
        }else {
            dt.code = dt.FLAG_FAIL;
            dt.msg = "修改失败";
        }
        closeout();
    }



    @RequestMapping(value="/modifyaudit")
    public void modifyApplyById(
            Integer borrowid,
            HttpServletRequest request,
            HttpServletResponse response,
            Model model) throws IOException{

        BorrowDAO borrowao = DAOFactory.getBorrowDAO();
        if (borrowao.modifyBorrowById(borrowid,2)) {
            dt.code = dt.FLAG_SUCC;
            dt.msg = "修改成功";
        }else {
            dt.code = dt.FLAG_FAIL;
            dt.msg = "修改失败";
        }
        closeout();
    }


    @RequestMapping(value="/getborrowlistByPage")//映射路径,拦截post请求
    public void getBorrowlistByPage(
            String equipname,
            String type,
            Integer audit,
            Integer page,
            Integer limit,//一页多少条
            HttpServletRequest request,
            HttpServletResponse response,
            Model model) throws IOException{

        Expression exp = new Expression();
        if(equipname != null && !equipname.equals("")){
            exp.andEqu("equipname", equipname, String.class);}
        if(type != null && !type.equals("")){
            exp.andEqu("type", type, String.class);}
        if (audit !=null && !audit.equals("")){
            exp.andEqu("audit",audit,Integer.class);
        }
        String wherecondition = exp.toString();

        BorrowDAO borrowao = DAOFactory.getBorrowDAO();

        List<VBorrowEquipment> list = borrowao.getBorrowListByPage(wherecondition,page, limit);
        int size = borrowao.getBorrowAmount();
        if (list!=null) {
            dt.code = dt.FLAG_SUCC;
            dt.msg = "查询成功，共查出" + dt.count;
            dt.data = list;
            dt.count = size;
        }else{
            dt.code = dt.FLAG_FAIL;
            dt.msg = "查询失败";
        }
        closeout();
    }

    @RequestMapping(value="/getborrowByStatus")//映射路径,拦截post请求
    public void getBorrowListBystatus(
            String equipname,
            String type,
            String rolename,
            Integer page,
            Integer limit,//一页多少条

            HttpServletRequest request,
            HttpServletResponse response,
            Model model) throws IOException{
        Expression exp = new Expression();
        if(equipname != null && !equipname.equals("")){
            exp.andEqu("equipname", equipname, String.class);}
        if(type != null && !type.equals("")){
            exp.andEqu("type", type, String.class);}
        if (rolename !=null && !rolename.equals("")){
            exp.andEqu("rolename",rolename,String.class);
        }
        String wherecondition = exp.toString();
        BorrowDAO borrowao = DAOFactory.getBorrowDAO();
        List<VBorrowEquipment> list = borrowao.getBorrowByStatus(1,page, limit);
        int size = borrowao.getBorrowAmount();
        if (list!=null) {
            dt.code = dt.FLAG_SUCC;
            dt.msg = "查询成功，共查出" + dt.count;
            dt.data = list;
            dt.count = size;
        }else{
            dt.code = dt.FLAG_FAIL;
            dt.msg = "查询失败";
        }
        closeout();
    }
    @RequestMapping(value="/getborrowByUserid")//映射路径,拦截post请求
    public void getApplyListByUser(
            Integer page,
            Integer limit) throws IOException{

        VUserRole user = (VUserRole)session.getAttribute("loginuser");
        String userid =user.getUserid();
        BorrowDAO borrowao = DAOFactory.getBorrowDAO();
        List<VBorrowEquipment> list = borrowao.getBorrowByUserid(userid,page,limit);
        int size = borrowao.getAmountByuser(userid);
        if (list!=null) {
            dt.code = dt.FLAG_SUCC;
            dt.msg = "查询成功，共查出" + dt.count;
            dt.data = list;
            dt.count = size;
        }else{
            dt.code = dt.FLAG_FAIL;
            dt.msg = "查询失败";
        }
        closeout();
    }
}
